package com.knowswift.account.controller;

import com.knowswift.common.bean.system.po.AppVersion;
import com.knowswift.common.common.BaseResponse;
import com.knowswift.common.utils.JwtTokenUtils;
import com.knowswift.common.utils.WrappedBeanCopier;
import com.knowswift.log.aop.AopLog;
import com.knowswift.security.account.User;
import com.knowswift.security.account.UserVO;
import com.knowswift.security.token.JwtAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/user/")
public class UserUserController extends BaseController {

    @PostMapping("/checkTokenStatus")
    @AopLog(saveLog = false, name = "checkToken", platform = "用户端")
    public BaseResponse checkTokenStatus(Authentication authentication) {

        JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
        User user = (User) authentication.getPrincipal();
        String token = jwtAuthenticationToken.getToken();
        if (JwtTokenUtils.isTokenAlmostExpired(token)) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("tokenVersion", user.getAppTokenVersion());
            token = JwtTokenUtils.generateToken(claims, user.getId());
        }
        UserVO userVO = WrappedBeanCopier.copyProperties(user, UserVO.class);
        userVO.setToken(token);
        return BaseResponse.success(userVO);
    }

    @PostMapping("/updateInformation")
    @AopLog(name = "修改信息", platform = "用户端")
    public BaseResponse updateInformation(@AuthenticationPrincipal User user, @RequestBody UserVO param) {
        if (param.getPushCid() != null) {
            user.setPushCid(param.getPushCid());
        }
        if (param.getReceivePush() != null) {
            user.setReceivePush(param.getReceivePush());
        }
        if (param.getLocationLatitude() != null) {
            user.setLocationLatitude(param.getLocationLatitude());
        }
        if (param.getLocationLongitude() != null) {
            user.setLocationLongitude(param.getLocationLongitude());
        }
        userService.updateById(user);
        return BaseResponse.success();
    }

    @PostMapping("/logout")
    @AopLog(name = "退出登录", platform = "用户端")
    public BaseResponse logout(@AuthenticationPrincipal User user) {
        user.setPushCid("");
        user.setAppTokenVersion(0);
        userService.updateById(user);
        return BaseResponse.success();
    }

    @PostMapping("/checkVersion")
    @AopLog(name = "检查版本", platform = "用户端")
    public BaseResponse checkVersion(HttpServletRequest request, @RequestBody AppVersion param) {
        String platform = request.getHeader("platform");
        AppVersion appVersion = appVersionService.lambdaQuery()
                .eq(AppVersion::getPlatform, platform)
                .gt(AppVersion::getVersionCode, param.getVersionCode())
                .orderByDesc(AppVersion::getVersionCode)
                .last(" limit 1").one();
        return BaseResponse.success(appVersion);
    }
}
